package com.mikel.monitor.controllers;
import com.mikel.monitor.domain.ZbNetworkState;

import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.roo.addon.web.mvc.controller.json.RooWebJson;

@RequestMapping("/zbnetworkstate")
@Controller
@RooWebScaffold(path = "zbnetworkstate", formBackingObject = ZbNetworkState.class)
@RooWebJson(jsonObject = ZbNetworkState.class)
public class ZbNetworkController {

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
    public String delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
        ZbNetworkState zbNetworkState = zbNetworkStateService.findZbNetworkState(id);
        zbNetworkStateService.deleteZbNetworkState(zbNetworkState);
        uiModel.asMap().clear();
        uiModel.addAttribute("page", (page == null) ? "1" : page.toString());
        uiModel.addAttribute("size", (size == null) ? "10" : size.toString());
        //        return "/zbnetworkstate?page=1&amp;size=10&amp;sortFieldName=fecha&amp;sortOrder=DESC";
        return "redirect:/zbnetworkstate";
    }
    
    @RequestMapping(
    		value="/simpleList",
    		produces = "text/html")
    public String simpleList(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, @RequestParam(value = "sortFieldName", required = false) String sortFieldName, @RequestParam(value = "sortOrder", required = false) String sortOrder, Model uiModel) {
        if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            uiModel.addAttribute("zbnetworkstates", ZbNetworkState.findZbNetworkStateEntries(firstResult, sizeNo, sortFieldName, sortOrder));
            float nrOfPages = (float) zbNetworkStateService.countAllZbNetworkStates() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            uiModel.addAttribute("zbnetworkstates", ZbNetworkState.findAllZbNetworkStates(sortFieldName, sortOrder));
        }
        addDateTimeFormatPatterns(uiModel);
        return "zbnetworkstate/simpleList";
    }
}
